<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>员工列表</title>
    <link rel="stylesheet" th:href="@{/layuiadmin/layui/css/layui.css}" href="../static/layuiadmin/layui/css/layui.css"
          media="all"/>
    <link rel="stylesheet" th:href="@{/layuiadmin/style/admin.css}" href="../static/layuiadmin/layui/css/layui.css"
          media="all"/>
    <style>
        body{
            margin: 10px;
            background: #eeeeee;
        }
    </style>
</head>
<body>
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">查询条件</label>
                    <div class="layui-input-inline" style="width:100px;">
                        <input type="text" name="loginCode" id="loginCode" placeholder="账号" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <div class="layui-input-inline" style="width:100px;">
                        <input type="text" name="name" id="name" placeholder="姓名"  autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <div class="layui-input-inline" style="width:100px;">
                        <input type="text" name="idNumber" placeholder="身份证号" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <div class="layui-input-inline" style="width:100px;">
                        <input type="text" id="test1" lay-key="1" name="birthday" placeholder="生日" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <div class="layui-input-inline" style="width: 150px">
                        <label class="layui-form-label" style="width:40px;;padding: 9px 0px">性别</label>
                        <div class="layui-input-block" style="margin-left:50px;">
                            <select name="gender">
                                <option value="">不限</option>
                                <option value="男">男</option>
                                <option value="女">女</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="layui-inline">
                    <div class="layui-input-inline" style="width: 180px">
                        <label class="layui-form-label" style="width:40px;;padding: 9px 0px">角色</label>
                        <div class="layui-input-block" style="margin-left:50px;">
                            <select name="roleId">
                                <option value="">不限</option>
                                <th:block th:each="role:${roleList}">
                                    <option th:value="${role.roleId}" th:text="${role.roleName}"></option>
                                </th:block>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="layui-inline">
                    <div class="layui-input-inline" style="width:200px;">
                        <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="Search">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <script type="text/html" id="test-table-toolbar-toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>
                    </button>
                    <button class="layui-btn layui-btn-sm" lay-event="update"><i class="layui-icon layui-icon-edit"></i>
                    </button>
                    <button class="layui-btn layui-btn-sm" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>
                    </button>
                </div>
            </script>
            <!-- table表格-->
            <table id="tableList" lay-filter="tableList"></table>
            <!--右边工具栏-->
            <script type="text/html" id="barDemo">
                <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="resetPwd">重置密码</a>
                <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </script>
        </div>
    </div>
    <script th:src="@{/layuiadmin/layui/layui.js}" src="../static/layuiadmin/layui/layui.js"></script>
    <script th:inline="javascript">
        layui.use(['table','laydate'], function () {
            var $ = layui.$
                ,form = layui.form
                ,table = layui.table
                ,laydate = layui.laydate;

            //执行一个 table 实例
            table.render({
                elem: '#tableList'
                , url: "/restaurant/user/userlist.do" //数据接口
                , method: 'post'
                , request: {
                    pageName: 'pageNum' //页码的参数名称，默认：page
                    , limitName: 'pageSize' //每页数据量的参数名，默认：limit
                }
                , response: {
                    statusCode: 200
                }
                , limits: [10, 20,30,40,50,60]
                , title: '用户表'
                , page: true //开启分页
                , even: true//开启背景隔行
                ,height:540
                , toolbar: '#test-table-toolbar-toolbarDemo' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
                , cols: [[ //表头
                    {type: 'checkbox', fixed: 'left'}
                    , {field: 'userId', title: 'ID',align:'center', width:160, sort: true, fixed: 'left',sort: true,hide:true}
                    , {field: 'loginCode', title: '账号',align:'center', width: 150}
                    , {field: 'name', title: '姓名',align:'center', width: 100, sort: true}
                    , {field: 'gender', title: '性别',align:'center', width: 100,}
                    , {field: 'phone', title: '电话',align:'center', width: 160,sort: true}
                    , {field: 'email', title: '邮箱',align:'center', width: 200}
                    , {field: 'birthday', title: '生日',align:'center', width: 150}
                    , {field: 'idNumber', title: '身份证号',align:'center', width: 200}
                    , {field: 'address', title: '家庭住址',align:'center', width: 160, sort: true}
                    /*取嵌套json数据用模板 templet: '<div>{{d.role.roleName}}</div>'*/
                    , {field: 'role', title: '角色',align:'center', width: 100, templet: '<div>{{d.role.roleName}}</div>'}
                    , {fixed: 'right', title: "工具栏",align:'center', width: 200, toolbar: '#barDemo'}
                ]]
            });

            //监听头工具栏事件
            table.on('toolbar(tableList)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id)
                    , data = checkStatus.data; //获取选中的数据
                switch (obj.event) {
                    case 'add':
                        layer.open({
                            type: 2
                            ,title: '添加用户'
                            ,content: '/restaurant/user/adduser.html'
                            ,maxmin: true
                            ,area: ['700px', '510px']
                            ,btn: ['确定', '取消']
                            ,yes: function(index, layero){
                                var iframeWindow = window['layui-layer-iframe'+ index]
                                    //该id是窗口打开html页面中表单提交按钮的id
                                    ,submitID = 'LAY-user-front-submit'
                                    ,submit = layero.find('iframe').contents().find('#'+ submitID);

                                //监听提交
                                iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                                    var field = data.field; //获取提交的字段
                                    //提交 Ajax 成功后，静态更新表格中的数据
                                    $.ajax({
                                        url: "/restaurant/user/adduser.do",
                                        type: "post",
                                        dataType: "JSON",
                                        data: data.field,
                                        success: function(result) {
                                            if(result.code == 200) {
                                                table.reload('tableList'); //数据刷新
                                                layer.close(index); //关闭弹层
                                            }else {
                                                layer.alert(result.msg, {
                                                    title: '提交结果'
                                                    ,anim: 6
                                                });
                                            }
                                        }
                                    });
                                });
                                submit.trigger('click');
                            }
                        });
                        break;
                    case 'update':
                        if (data.length === 0) {
                            layer.msg('请选择一行');
                        } else if (data.length > 1) {
                            layer.msg('只能同时编辑一个');
                        } else {
                            var userId = checkStatus.data[0].userId;
                            var url = '/restaurant/user/edituser.html/'+userId;
                            layer.open({
                                type: 2
                                ,title: '编辑用户'
                                ,content:url
                                ,maxmin: true
                                ,area: ['700px', '460px']
                                ,btn: ['确定', '取消']
                                ,yes: function(index, layero){
                                    var iframeWindow = window['layui-layer-iframe'+ index]
                                        ,submitID = 'LAY-user-front-submit'
                                        ,submit = layero.find('iframe').contents().find('#'+ submitID);

                                    //监听提交
                                    iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                                        var field = data.field; //获取提交的字段
                                        //提交 Ajax 成功后，静态更新表格中的数据
                                        $.ajax({
                                            url: "/restaurant/user/saveEdit.do",
                                            type: "post",
                                            dataType: "JSON",
                                            data:field,
                                            success: function(result) {
                                                if(result.code == 200) {
                                                    table.reload('tableList'); //数据刷新
                                                    layer.close(index); //关闭弹层
                                                }else {
                                                    layer.alert(result.msg, {
                                                        title: '提交结果'
                                                        ,anim: 6
                                                    });
                                                }
                                            }
                                        });
                                    });
                                    submit.trigger('click');
                                }
                            });
                        }
                        break;
                    case 'delete':
                        if (data.length === 0) {
                            layer.msg('请选择一行');
                        } else {
                            layer.confirm('确定删除?', function (){
                                var ids = '';
                                for (var i = 0; i < data.length; i++) {
                                    ids += data[i].userId + ",";
                                }
                                $.ajax({
                                    url: "/restaurant/user/del.do",
                                    type: "post",
                                    dataType: "JSON",
                                    data: {ids:ids},
                                    success: function(result) {
                                        if(result.code == 200) {
                                            table.reload('tableList'); //数据刷新
                                            layer.msg('删除成功!', {icon: 6});
                                        }else {
                                            layer.alert(result.msg, {
                                                title: '提交结果'
                                                ,anim: 6
                                            });
                                        }
                                    }
                                });
                            });

                        }
                        break;
                };
            });

            //监听行工具事件
            table.on('tool(tableList)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
                var data = obj.data //获得当前行数据
                    , layEvent = obj.event; //获得 lay-event 对应的值
                if (layEvent === 'resetPwd') {
                    layer.confirm('确定要重置用户【'+data.name+'】的密码?', function (index) {
                        $.ajax({
                            url: "/restaurant/user/resetpwd/"+data.userId,
                            dataType: "JSON",
                            success: function(result) {
                                if(result.code == 200) {
                                    layer.msg('重置成功!', {icon: 6});
                                }else {
                                    layer.alert(result.msg, {
                                        title: '提交结果'
                                        ,anim: 6
                                    });
                                }
                            }
                        });
                        layer.close(index);
                    });

                } else if (layEvent === 'del') {
                    layer.confirm('确定删除?'+data.name, function (index) {
                        $.ajax({
                            url: "/restaurant/user/del.do",
                            type: "post",
                            dataType: "JSON",
                            data: {ids:data.userId},
                            success: function(result) {
                                if(result.code == 200) {
                                    obj.del(); //删除对应行（tr）的DOM结构
                                    layer.msg('删除成功!', {icon: 6});
                                }else {
                                    layer.alert(result.msg, {
                                        title: '提交结果'
                                        ,anim: 6
                                    });
                                }
                            }
                        });
                        layer.close(index);
                        //向服务端发送删除指令
                    });
                } else if (layEvent === 'edit') {
                    var url = '/restaurant/user/edituser.html/'+data.userId;
                    layer.open({
                        type: 2
                        ,title: '编辑用户'
                        ,content:url
                        ,maxmin: true
                        ,area: ['750px', '460px']
                        ,btn: ['确定', '取消']
                        ,yes: function(index, layero){
                            var iframeWindow = window['layui-layer-iframe'+ index]
                                ,submitID = 'LAY-user-front-submit'
                                ,submit = layero.find('iframe').contents().find('#'+ submitID);

                            //监听提交
                            iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                                var field = data.field; //获取提交的字段
                                //提交 Ajax 成功后，静态更新表格中的数据
                                $.ajax({
                                    url: "/restaurant/user/saveEdit.do",
                                    type: "post",
                                    dataType: "JSON",
                                    data:field,
                                    success: function(result) {
                                        if(result.code == 200) {
                                            table.reload('tableList'); //数据刷新
                                            layer.close(index); //关闭弹层
                                        }else {
                                            layer.alert(result.msg, {
                                                title: '提交结果'
                                                ,anim: 6
                                            });
                                        }
                                    }
                                });
                            });
                            submit.trigger('click');
                        }
                    });
                }
            });
            //监听搜索
            form.on('submit(Search)', function(data){
                var field = data.field;
                //执行重载
                table.reload('tableList', {
                    where: field
                    ,page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
            });

            //日期选择器
            laydate.render({
                elem: '#test1'
                ,format: 'yyyy-MM-dd' //可任意组合
            });
        });
    </script>
</body>
</html>